package phoenix.mob_android.page_objects;

import org.openqa.selenium.WebElement;

import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.PageBaseMobAndroidExt;
import phoenix.core.config.extensions.ConfigExt;
import phoenix.mob_android.locators.base.SearchPageLocatorsBase;

public class SearchPage extends PageBaseMobAndroidExt
{
	/**
	 * Initializes the SearchPage
	 * @param p_manager
	 *            PhoenixManager instance
	 */
	public SearchPage(PhoenixManager p_manager)
	{
		super(p_manager);
	}

	/**
	 * Gets web elements by site
	 * @return IndexPageLocatorsBase instance
	 */
	public SearchPageLocatorsBase elements()
	{
		switch (manager.testModel().getSiteName())
		{
			case ConfigExt.SITE_ID_FLIRT:
			{
				return new phoenix.mob_android.locators.flirt.SearchPageLocators();
			}
			case ConfigExt.SITE_ID_WILDBUDDIES:
			{
				return new phoenix.mob_android.locators.wildbuddies.SearchPageLocators();
			}
			case ConfigExt.SITE_ID_CHEECKYLOVERS:
			{
				return new phoenix.mob_android.locators.cheeckylovers.SearchPageLocators();
			}
			default:
			{
				return null;
			}
		}
	}

	public boolean isOnSearchPage()
	{
		md().waitForPresence(elements().blockSearch, "Block search results doesn't appear");
		return md().isElementPresent(elements().blockSearch);
	}

	public SearchPage isOnGridSearchPage()
	{
		md().waitForPresence(elements().blockSearchResultGrid, "Block search result grid doesn't appear");

		return this;
	}

	public SearchPage isOnMapSearchPage()
	{
		md().waitForPresence(elements().blockSearchResultMap, "Block search result map doesn't appear");

		return this;
	}

	public SearchPage clickMapButton()
	{
		md().waitForPresence(elements().btnMap, "Button map doesn't appear");
		md().click(elements().btnMap);

		return this;
	}

	public void openProfileFromGridSearch(int p_numberProfile)
	{
		md().wait(5);
		manager.wd().driver().findElements(elements().blockUsersFromGridSearch).get(p_numberProfile).click();
	}

	public SearchPage openSearchParameters()
	{
		md().click(elements().btnSearchParams);

		return this;
	}

	public SearchPage submitSearchParameters()
	{
		md().click(elements().btnSubmitSearchParameters);
		md().wait(6);

		return this;
	}

	public void checkForSearchResults()
	{
		md().waitForPresence(elements().blockUserSearchScreenname, "User screenname in the search results doesn't appear");
		md().waitForPresence(elements().blockUserSearchLocation, "User location in the search results doesn't appear");
		md().waitForPresence(elements().blockUserSearchPhoto, "User photo in the search results doesn't appear");
	}

	public void openPrivateChat()
	{
		md().waitForVisibility(elements().btnPrivateChat, "Private chat button doesn't appear");
		md().click(elements().btnPrivateChat);
	}

	public Object isWinkSend()
	{
		WebElement p_wink = manager.wd().driver().findElement(elements().btnWink);
		Object attribute = p_wink.getAttribute("enabled");

		return attribute;
	}

	public void makeWinkFromSearch()
	{
		md().waitForPresence(elements().btnWink, "Wink button doesn't appear");
		md().click(elements().btnWink);
		md().wait(5);
	}

	public SearchPage addToFavouriteFromSearch()
	{
		md().waitForPresence(elements().btnFavourite, "Favourite button doesn't appear");
		md().click(elements().btnFavourite);

		return this;
	}

	public String getScreennameOfUser()
	{
		return manager.string().getSubstringBefore(manager.wd().driver().findElements(elements().blockUserSearchScreenname).get(0).getText(), ",");
	}

	public SearchPage chooseGender(String p_gender)
	{
		md().waitForPresence(elements().btnGenderMale, "Button gender male doesn't appear");
		switch (p_gender)
		{
			case "male":
			{
				md().click(elements().btnGenderMale);
				break;
			}

			case "female":

			{
				md().click(elements().btnGenderFemale);
				break;
			}

			default:
			{
				log().errorAndScreen("Incorrect Gender specifyed for Registration");
			}
		}

		return this;
	}

	public SearchPage clickSingleButton()
	{
		md().waitForPresence(elements().btnSingle, "Button single doesn't appear");
		md().click(elements().btnSingle);

		return this;
	}

	public SearchPage clickGridButton()
	{
		md().wait(6);
		md().waitForVisibility(elements().btnGrid, "Button grid doesn't appear");
		md().click(elements().btnGrid);

		return this;
	}

	public void closeMobileNumberPopUp()
	{
		md().waitForVisibility(elements().btnClosePopUp, "Button close mobile pop-up doesn't appear");
		md().click(elements().btnClosePopUp);

	}

	public void clickUpgradeButton()
	{
		md().waitForPresence(elements().btnUpgrade, "Upgrade button doesn't appear in the search");
		md().click(elements().btnUpgrade);
	}

	public boolean checkForUpgradeButtonInSearch()
	{
		return md().isElementPresent(elements().btnUpgrade);
	}

	public SearchPage scrollDownAndCheckForUpgradeButton(int p_number)
	{
		int i = 0;
		while (i <= p_number)
		{
			md().swipe();
			if (this.checkForUpgradeButtonInSearch())
			{
				log().errorAndScreen("Upgrade button appear for Paid user");
			}
			i++;
		}

		return this;
	}

	public SearchPage scrollDownSearch()
	{

		for (int i = 0; i < 25; i++)
		{
			md().swipe();
			if (md().isElementPresent(elements().btnUpgrade))
			{
				break;
			}
		}
		return this;
	}
}
